package com.link2room.cms.service.impl;

import com.link2room.cms.dto.result.ArticleBaseDto;
import com.link2room.cms.entity.ArticleBase;
import com.link2room.cms.entity.ArticleDetail;
import com.link2room.cms.service.IArticleService;
import com.link2room.common.dao.ICommonDao;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sqj88
 * @date 2017/12/1 11:18
 */
@Service("articleService")
public class ArticleServiceImpl implements IArticleService {
    @Resource
    private ICommonService commonService;
    @Resource
    private ICommonDao commonDao;

    @Override
    @Transactional
    public void saveOrUpdateArticle(ArticleBase articleBase, ArticleDetail articleDetail)throws Exception{
        commonService.saveOrUpdate(articleBase);
        commonService.saveOrUpdate(articleDetail);
    }


    @Override
    @Transactional
    public void deleteArticle(String lhotelGroupCode,Long id)throws Exception{
        ArticleBase articleBase=commonService.findOneById(ArticleBase.class,id);
        String articleId=articleBase.getArticleId();
        ArticleDetail queryad=new ArticleDetail();
        queryad.setLhotelGroupCode(lhotelGroupCode);
        queryad.setArticleId(articleId);
        commonService.delete(ArticleBase.class,id);
        commonService.deleteAllEQ(ArticleDetail.class,queryad);
    }

    @Override
    public QueryResult<ArticleBaseDto> findArticleList(String lhotelGroupCode,
                                                       String categoryCode,
                                                       String isHalt,
                                                       String isAudit,
                                                       String language,
                                                       String articleType,
                                                       String keyword,
                                                       Integer currentPage,
                                                       Integer pageSize)throws Exception{
        StringBuffer querySb=new StringBuffer();
        StringBuffer countSb=new StringBuffer();
        querySb.append("SELECT\n" +
                "  a.`lhotel_group_code` lhotelGroupCode,\n" +
                "  a.`lhotel_code` lhotelCode,\n" +
                "  a.`article_type` articleType,\n" +
                "  a.`id`,\n" +
                "  a.article_id articleId,\n" +
                "  a.`category_code` categoryCode,\n" +
                "  a.`auto_audit` autoAudit,\n" +
                "  b.`language`,\n" +
                "  b.`title`,\n" +
                "  b.`url`,\n" +
                "  b.`template`,\n" +
                "  b.`ptitle`,\n" +
                "  b.`lit_pic` litPic,\n" +
                "  b.`pics`,\n" +
                "  b.`is_halt` isHalt,\n" +
                "  b.`descript` descript,\n" +
                "  b.`is_audit` isAudit\n" +
                "FROM\n" +
                "  `article_base` AS a\n" +
                "  LEFT JOIN `article_detail` AS b\n" +
                "    ON a.`lhotel_group_code` = b.`lhotel_group_code`\n" +
                "    AND a.`article_id` = b.`article_id`\n" +
                "WHERE a.`lhotel_group_code` = ?1 ");
        countSb.append("SELECT\n" +
                "  COUNT(*)\n" +
                "FROM\n" +
                "  `article_base` AS a\n" +
                "  LEFT JOIN `article_detail` AS b\n" +
                "    ON a.`lhotel_group_code` = b.`lhotel_group_code`\n" +
                "    AND a.`article_id` = b.`article_id`\n" +
                "WHERE a.`lhotel_group_code` = ?1 ");
        int i=1;
        List<Object> objects=new ArrayList<>();
        objects.add(lhotelGroupCode);
        if(StringUtil.isNotBlank(categoryCode)){
            i++;
            querySb.append("   AND a.`category_code` = ?"+i);
            countSb.append("   AND a.`category_code` = ?"+i);
            objects.add(categoryCode);
        }
        if(StringUtil.isNotBlank(isHalt)){
            i++;
            querySb.append("   AND b.`is_halt` = ?"+i);
            countSb.append("   AND b.`is_halt` = ?"+i);
            objects.add(isHalt);
        }
        if(StringUtil.isNotBlank(language)){
            i++;
            querySb.append("   AND b.`language` = ?"+i);
            countSb.append("   AND b.`language` = ?"+i);
            objects.add(language);
        }
        if(StringUtil.isNotBlank(isAudit)){
            i++;
            querySb.append("   AND b.`is_audit` = ?"+i);
            countSb.append("   AND b.`is_audit` = ?"+i);
            objects.add(isAudit);
        }

        if(StringUtil.isNotBlank(articleType)){
            i++;
            querySb.append("  AND FIND_IN_SET(?"+i+", a.`article_type`)");
            countSb.append("  AND FIND_IN_SET(?"+i+", a.`article_type`)");
            objects.add(articleType);
        }
        if(StringUtil.isNotBlank(keyword)){
            i++;
            querySb.append("   AND b.`title` LIKE ?"+i);
            countSb.append("   AND b.`title` LIKE ?"+i);
            objects.add("%"+keyword+"%");
        }
        QueryResult<ArticleBaseDto> articleBaseDtoQueryResult = commonService.findByNative(querySb.toString(),countSb.toString(), objects, objects, ArticleBaseDto.class, currentPage, pageSize);
        return articleBaseDtoQueryResult;
    }

    @Override
    @Transactional
    public void updateClickCount(String lhotelGroupCode,
                                 String articleId,
                                 String language)throws Exception{
        StringBuffer sb=new StringBuffer();
        sb.append("UPDATE article_detail SET click_count=click_count+1 WHERE lhotel_group_code=?1 AND article_id=?2  AND language=?3");
        List<Object> objects=new ArrayList<>();
        objects.add(lhotelGroupCode);
        objects.add(articleId);
        objects.add(language);
        commonDao.excuteByNative(sb.toString(),objects);
    }
}
